package com.example.qu_tao.View;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.qu_tao.R;
import com.example.qu_tao.Util.CaiGouDataManager;
import com.example.qu_tao.entity.CaiGouInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class FabuCaiGouActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    // 表单控件
    private Spinner spinnerType, spinnerVariety, spinnerUnit, spinnerSource, spinnerCycle, spinnerRegion;
    private EditText editQuantity, editDescription;
    private Button btnPublish;
    private ImageView back;
    // 数据源
    private String[] typeOptions = {"农产品", "工业品", "日用品", "食品", "其他"};
    private String[] varietyOptions = {"不限", "蔬菜", "水果", "粮食", "肉类", "水产品"};
    private String[] unitOptions = {"斤", "公斤", "吨", "千克", "克", "个", "桶"};
    private String[] sourceOptions = {"全国", "华北", "华东", "华南", "华中", "西北", "东北", "西南"};
    private String[] cycleOptions = {"一次性", "周采购", "月采购", "季采购", "年采购"};
    private String[] regionOptions = {"请选择地区", "北京", "上海", "广东", "江苏", "浙江", "山东", "四川", "其他"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facaigou);

        // 初始化控件
        initViews();

        // 初始化下拉框适配器
        initSpinners();

        // 初始化发布按钮点击事件
        initPublishButton();
    }

    private void initViews() {
        spinnerType = findViewById(R.id.spinner_type);
        spinnerVariety = findViewById(R.id.spinner_variety);
        spinnerUnit = findViewById(R.id.spinner_unit);
        spinnerSource = findViewById(R.id.spinner_source);
        spinnerCycle = findViewById(R.id.spinner_cycle);
        spinnerRegion = findViewById(R.id.spinner_region);
        editQuantity = findViewById(R.id.edit_quantity);
        editDescription = findViewById(R.id.edit_description);
        btnPublish = findViewById(R.id.btn_publish);
        back = findViewById(R.id.btn_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void initSpinners() {
        // 货品类型适配器
        ArrayAdapter<String> typeAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, typeOptions);
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerType.setAdapter(typeAdapter);
        spinnerType.setOnItemSelectedListener(this);

        // 货品品种适配器
        ArrayAdapter<String> varietyAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, varietyOptions);
        varietyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerVariety.setAdapter(varietyAdapter);
        spinnerVariety.setOnItemSelectedListener(this);

        // 单位适配器
        ArrayAdapter<String> unitAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, unitOptions);
        unitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerUnit.setAdapter(unitAdapter);
        spinnerUnit.setOnItemSelectedListener(this);

        // 货源地适配器
        ArrayAdapter<String> sourceAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, sourceOptions);
        sourceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSource.setAdapter(sourceAdapter);
        spinnerSource.setOnItemSelectedListener(this);

        // 采购周期适配器
        ArrayAdapter<String> cycleAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, cycleOptions);
        cycleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCycle.setAdapter(cycleAdapter);
        spinnerCycle.setOnItemSelectedListener(this);

        // 收货地区适配器
        ArrayAdapter<String> regionAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, regionOptions);
        regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerRegion.setAdapter(regionAdapter);
        spinnerRegion.setOnItemSelectedListener(this);
    }

    private void initPublishButton() {
        btnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 验证表单数据
                if (validateForm()) {
                    // 收集表单数据
                    CaiGouInfo caiGouInfo = collectFormData();

                    // 保存采购信息
                    CaiGouDataManager.getInstance().addCaiGouInfo(caiGouInfo);

                    // 返回采购管理页面并刷新
                    Intent intent = new Intent();
                    intent.putExtra("refresh", true);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    private boolean validateForm() {
        String quantity = editQuantity.getText().toString().trim();
        if (quantity.isEmpty()) {
            Toast.makeText(this, "请输入需求量", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (spinnerRegion.getSelectedItemPosition() == 0) {
            Toast.makeText(this, "请选择收货地区", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    private CaiGouInfo collectFormData() {
        CaiGouInfo caiGouInfo = new CaiGouInfo();

        // 设置基本信息
        caiGouInfo.setType(spinnerType.getSelectedItem().toString());
        caiGouInfo.setVariety(spinnerVariety.getSelectedItem().toString());
        caiGouInfo.setQuantity(editQuantity.getText().toString());
        caiGouInfo.setUnit(spinnerUnit.getSelectedItem().toString());
        caiGouInfo.setSource(spinnerSource.getSelectedItem().toString());
        caiGouInfo.setCycle(spinnerCycle.getSelectedItem().toString());
        caiGouInfo.setRegion(spinnerRegion.getSelectedItem().toString());
        caiGouInfo.setDescription(editDescription.getText().toString());

        // 设置发布时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        caiGouInfo.setPublishTime(sdf.format(new Date()));

        // 生成唯一ID
        caiGouInfo.setId(System.currentTimeMillis() + "");

        return caiGouInfo;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // 下拉框选择变化处理
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // 未选择处理
    }
}